package wangjg.android;

import wangjg.android.media.voice.MyRecordThread;
import wangjg.android.media.voice.OutputDisplay;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class VoiceActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new VoiceSView(this));
    }
}

class VoiceSView extends SurfaceView implements SurfaceHolder.Callback, OutputDisplay {
	private SurfaceHolder holder;
	private MyRecordThread rt = new MyRecordThread();
	
	public VoiceSView(Context context) {
		super(context);
		holder = this.getHolder();// 获取holder
		holder.addCallback(this);
		this.setOnClickListener(l);
		
	}
	
	OnClickListener l=new OnClickListener(){
		public void onClick(View v) {
			rt.shift();
		}  
    };
    
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		rt.setDisplay(this);
		rt.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		rt.stopRecord();
	}
	
	public void draw(short[] buffer, int offset, int len){
		Canvas canvas = holder.lockCanvas(null);
		
		int width = canvas.getWidth();
		int height = canvas.getHeight();
		Paint paint = new Paint();
		
		paint.setColor(Color.BLACK);
		canvas.drawRect(new RectF(0, 0, width, height), paint);

		paint.setColor(Color.BLUE);
		int cy = height / 2;
		for(int i=offset;i<len;i++){
			int v = ((int)buffer[i]);
			canvas.drawLine(i, cy, i, cy-v, paint);
		}
		
		holder.unlockCanvasAndPost(canvas);
	}

}
